<template>
	<view class="wrap">
		<!-- v-if="model.podid" -->
		<view >
			<js-error mode="bottom" ref="jsError"></js-error>
			<u-sticky class="u-sticky">
				<view class="cu-bar search" style="padding: 10px">
					<u-search style="flex: 1;" v-model="barCode" ref="uSearch" :focus="focus" placeholder="请先聚焦后扫描"
						:show-action="false" @search="confirm"></u-search>
					<view style="margin-left: 10px; display: flex; flex-direction: column">
						<u-icon @click="search" name="scan" size="50"></u-icon>
					</view>
				</view>
			</u-sticky>
			<view class="cu-form-group">
				<view class="title">发货单号：</view>
				<view style="flex: 1;"> {{ model.soCode|| ""  }} </view>
			</view>
			<view class="cu-form-group">
				<view class="title">发货车次/车牌：</view>
				<view style="flex: 1;"> {{ model.mfCarplanFhH&&model.mfCarplanFhH.djNo ? model.mfCarplanFhH.djNo : ''  }} {{ model.mfCarplanFhH&&model.mfCarplanFhH.carNo ? '/ '+ model.mfCarplanFhH.carNo : '' }}</view>
			</view>
			<view class="cu-form-group" style="position: relative;">
				<view class="title">存货编码：</view>
				<view style="flex: 1;">{{ model.invCode|| ""  }}</view>
				<view v-if="invFlag" @click="handleFocus"
					style="text-align: center;position: fixed;top: 80rpx;left: 20rpx;margin: 0 20rpx;z-index: 9999;">
					<u-icon name="checkmark-circle" color="#81b337" size="60"></u-icon>
					<view style="color: #81b337"> 验证通过 </view>
				</view>
				<view v-if="!invFlag" @click="handleFocus"
					style="text-align: center;position: fixed;top: 80rpx;left: 20rpx;margin: 0 20rpx;;z-index: 9999;">
					<u-icon name="info-circle" color="#ff9900" size="60"></u-icon>
					<view style="color: #ff9900"> 暂未验证 </view>
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">名称规格：</view>
				<view style="flex: 1;">{{ model.invName|| ""  }}{{ model.invStd ? '('+model.invStd +')' : ''  }}</view>
			</view>
			<view class="cu-form-group">
				<view class="title">批次：</view>
				<view style="flex: 1;">{{ model.ibatch|| ""  }}</view>
			</view>
			
			
			<view class="cu-form-group">
				<view class="title">备注：</view>
				<view style="flex: 1;"> {{ model.remarks|| ""  }}</view>
			</view>
			<!-- <view class="cu-form-group">
				<view class="title">剩余/计划重量：</view>
				<view style="flex: 1;"> {{ model.syFhQty|| ""  }}{{ model.planWeight ? '/'+model.planWeight  : ''  }}
					{{ model.basInv&&model.basInv.unitName ? '('+ model.basInv.unitName +')' : '' }}
					
					</view>
			</view>
			<view class="cu-form-group  text-bold text-red">
				<view class="title">本次发货总数：</view>
				<view style="flex: 1;"> {{ fhQty || "0"  }}</view>
			</view> -->
			<view class="action bg-white" 
				style="font-size: 20px;align-items: center;display: flex;padding: 0 10px 10px 10px;">
				<u-icon name="/static/image/detail.png" size="80"></u-icon>
				<text class="cuIcon-titles text-bold">货位信息</text>
				<view style="flex: 1;text-align: right;">
					<button class="cu-btn lines-green shadow-blur" @tap="historyList(model.id)">
						装车历史
					</button>
				</view>				
			</view>
			<view style="padding: 10px;">
				<view class="cu-form-group">
					<view class="title" style="padding-left: 30rpx;">仓库：</view>
					<view style="flex: 1;">{{ model.basWare&&model.basWare.cwhname ? model.basWare.cwhname : '' }}</view>
				</view>
				<view class="cu-form-group " v-if="model.basWare&&model.basWare.bwhpos == '1'">
					<view class="title"><text class="cuIcon-favorfill text-xs text-red"></text>货位：</view>
					<view style="flex: 1;">
						<u-input placeholder="请选择" v-model="model.posName" :disabled="true" di
							maxlength="200"  type="select" @click="hwxz()" ></u-input>
					</view>
				</view>
				
					
				
			</view>
			<view class="action bg-white"
				style="font-size: 20px;align-items: center;display: flex;padding: 0 10px 10px 10px;">
				<u-icon name="/static/image/detail.png" size="80"></u-icon>
				<text class="cuIcon-titles text-bold">数量信息</text>
				<!-- <view style="flex: 1;text-align: right;" >
					<u-icon @click="GoaddsoOutListChild()" name="plus-circle" size="90" color="#3E97B0"></u-icon>
				</view>	 -->			
			</view>
			<!-- style="padding: 10px;" -->
			<view >
				<view v-for="(item,index) in soOutList" :key="index" class="cu-item shadow " >
					<view class="cu-form-group"
						style="display: flex;justify-content: space-between;">
						<view
							style=" display: inline-block;padding: 5px;background: #3E97B0;color: #fff;min-width: 40px;text-align: center;font-style: italic;font-weight: bold">
							{{ index + 1 }}
						</view>
						<view class="" v-if="index == 0 && !!model.ibatch">
							
						</view>
						<view v-else>
							<text v-if="(model.basWare && model.basWare.bwhpos == '1') || (model.basInv &&model.basInv.binvbatch == '1')"
								style="font-size: 35px;" class="cuIcon-deletefill text-sl text-red"
								@tap="delsoOutDetail(item,index)"></text>
						</view>
					</view>
					<view class="cu-form-group" v-if="model.basInv&&model.basInv.binvbatch == '1'">
						<view class="title"><text class="cuIcon-favorfill text-xs text-red"></text>批次：</view>
						<u-input v-if="model.basWare && model.basWare.bwhpos == '1'" placeholder="请选择" v-model="item.cbatch" :disabled="true" di
							maxlength="200"  type="select" @click="xzhw(index,item)" ></u-input>
						<u-input v-else placeholder="请选择" v-model="item.cbatch" :disabled="true" di type="select" @click="xzCbatch(index,item)" ></u-input>
						
						<!-- <view style="flex: 1;" v-if="model.basWare && model.basWare.bwhpos == '1'">
							<u-input placeholder="请选择" v-model="item.cbatch" :disabled="true" di type="select" @click="xzhw(index,item)"></u-input>
						</view>
						<view style="flex: 1;" v-else>
							
						</view> -->
					</view>
					<view class="cu-form-group " v-if="model.basInv&&model.basInv.bfixExch != '0'">
						<view class="title"><text class="cuIcon-favorfill text-xs text-red"></text>件数：</view>
						<view style="flex: 1;">
							<u-input placeholder="请输入" @input="inumChange($event,item,index)" v-model="item.inum"
								type="digit" di maxlength="200" ></u-input>
						</view>
					</view>
					<view class="cu-form-group ">
						<view class="title"><text class="cuIcon-favorfill text-xs text-red"></text>发货数量：</view>
						<view style="flex: 1;">
							<u-input placeholder="请输入" @blur="iqtyChange($event,item,index)" v-model="item.iqty"
								type="digit" di maxlength="200" :disabled="model.basInv&&model.basInv.bfixExch != '0'?true:false"></u-input>
						</view>
					</view>
					<view class="cu-form-group " v-if="model.needSoTrack ==1 ">
						<view class="title" style="padding-left: 30rpx;">已核对托盘：</view>
						<view style="flex: 1;">
							{{ item.tpSeqNumber || '' }}
						</view>
					</view>
					
					<view class="cu-form-group" v-if="model.basInv&&model.basInv.bfixExch != '0'">
						<view class="title" style="padding-left: 30rpx;">换算率：</view>
						<view style="flex: 1;">
							{{ item.ichangeRate || '' }}
						</view>
					</view>
					<view class="cu-form-group">
						<view class="title" style="padding-left: 30rpx;">品级：</view>
						<view style="flex: 1;">
							{{ item.rgrade || '' }}
						</view>
					</view>
					
					
					
				</view>
			</view>
			
			<!-- <view class="action bg-white"
			v-if="model.needSoTrack ==1"
				style="font-size: 20px;align-items: center;display: flex;padding: 0 10px 10px 10px;">
				<u-icon name="/static/image/detail.png" size="80"></u-icon>
				<text class="cuIcon-titles text-bold">托盘信息</text>
				<view style="flex: 1;text-align: right;" >
					<button class="cu-btn lines-green shadow-blur" @tap="show=true">
						查看
					</button>
				</view>
				
			</view> -->
			<u-popup v-model="show" mode="right" length="90%">
				<view
					style="font-size: 18px;border-bottom: 1px solid #aaa;font-weight: bold;padding: 10px;display: flex;justify-content: space-between;">
					<!-- <text class=" padding-left-sm" style="color: #3E97B0;"></text> -->
					<!-- <text @click="show=false">关闭</text> -->
					<text >托盘数量：{{sortedChildren.length}}</text>
				</view>
				<scroll-view scroll-y="true">
				  <view style="background-color: #FAFAFA;">
					<view v-for="(item,index) in sortedChildren" class="cu-item shadow " style="position: relative;" :key="index"
						v-if="item.status != '1'">
						<view v-if="model.basWare.bwhpos == '1' || model.basInv.binvbatch == '1'" class="cu-form-group"
							style="display: flex;justify-content: space-between;">
							<view
								style=" display: inline-block;padding: 5px;background: #3E97B0;color: #fff;min-width: 40px;text-align: center;font-style: italic;font-weight: bold">
								{{ index + 1 }}
							</view>
							<view style="flex: 1;margin-left: 10rpx;">
								<u-input placeholder="请扫码" v-model="item.cbatch" type="text" :disabled="true"></u-input>
							</view>
							<view class="">
								<text v-if="model.basWare.bwhpos == '1' || model.basInv.binvbatch == '1'"
									style="font-size: 35px;" class="cuIcon-deletefill text-sl text-red"
									@tap="delDetail(item,index)"></text>
							</view>
						</view>
						<!-- <view class="cu-form-group " >
							<view class="title1" style="padding-left: 30rpx;">托盘：</view>
							<view style="flex: 1;">
								<u-input placeholder="请扫码" v-model="item.cbatch" type="text" :disabled="true"></u-input>
							</view>
						</view> -->
					</view>
					<view class="cu-item shadow " v-if="palletRdsList.length>0">历史记录</view>
					<view v-for="(item,index) in palletRdsList" class="cu-item shadow " style="position: relative;" :key="index"
						v-if="item.status != '1'">
						<view v-if="model.basWare.bwhpos == '1' || model.basInv.binvbatch == '1'" class="cu-form-group"
							style="display: flex;justify-content: space-between;">
							<view
								style=" display: inline-block;padding: 5px;background: #3E97B0;color: #fff;min-width: 40px;text-align: center;font-style: italic;font-weight: bold">
								{{ index + 1 }}
							</view>
							<view style="flex: 1;margin-left: 10rpx;">
								<u-input placeholder="请扫码" v-model="item.cbatch" type="text" :disabled="true"></u-input>
							</view>
							<view class="">
								<!-- <text v-if="model.basWare.bwhpos == '1' || model.basInv.binvbatch == '1'"
									style="font-size: 35px;" class="cuIcon-deletefill text-sl text-red"
									@tap="delDetail(item,index)"></text> -->
							</view>
						</view>
						<!-- <view class="cu-form-group " >
							<view class="title1" style="padding-left: 30rpx;">托盘：</view>
							<view style="flex: 1;">
								<u-input placeholder="请扫码" v-model="item.cbatch" type="text" :disabled="true"></u-input>
							</view>
						</view> -->
					</view>
				</view>
				</scroll-view>
				<view class="btn-group cu-bar foot" >
					<button class="cu-btn lines-default shadow-blur" @click="show=false">
						关闭
					</button>
				</view>
			</u-popup>
			<view style="height: 130px;"></view>
			<!-- <view class="btn-group cu-bar foot">
				<button class="cu-btn lines-default shadow-blur" @tap="init()">
					重新装车
				</button>
				<button class="cu-btn lines-green shadow-blur" @tap="submit(vuex_config.FhStatusEnum.OVER)">
					结束装车
				</button>
				<button class="cu-btn bg-green shadow-blur" @tap="submit()">
					<text class="cuIcon-command" style="margin-right: 10rpx;"></text> 保存
				</button>
			</view> -->
			
			<view class="footer" >
				<view style="border-bottom:1px solid #eee ;padding: 10rpx 20rpx 0;display: flex;align-items: center;">
					<view class="">
					<view style="display: flex;" class="  text-bold text-red">
						<view style="width: 170rpx;">本次/剩余：</view>
						<view style="flex: 1;"> {{ fhQty || "0"  }}/{{ model.syFhQty|| ""  }}</view>
					</view>
				<!-- 	<view style="display: flex;">
						<view style="width: 220rpx;">剩余/计划重量：</view>
						<view style="flex: 1;"> {{ model.syFhQty|| ""  }}{{ model.planWeight ? '/'+model.planWeight  : ''  }}
							{{ model.basInv&&model.basInv.unitName ? '('+ model.basInv.unitName +')' : '' }}
							
							</view>
					</view> -->
					<view style="display: flex;margin: 10rpx 0;" >
						<view style="width: 170rpx;">  托盘核对  ：</view>
						<view style="flex: 1;"> {{ sortedChildren.length }}</view>
					</view>
					</view>
					<view style="flex: 1;text-align: right;" >
						<button class="cu-btn lines-green shadow-blur" @tap="show=true">
							托盘信息
						</button>
					</view>
				</view>
				<view style=" display: flex;justify-content: space-around;padding: 20rpx ;">
					<button class="cu-btn lines-default shadow-blur" @tap="init()">
						重新装车
					</button>
					<button style="margin: 0 10px;" class="cu-btn lines-green shadow-blur" @tap="submit(vuex_config.FhStatusEnum.OVER)">
						结束装车
					</button>
					<button class="cu-btn bg-green shadow-blur" @tap="submit()">
						<text class="cuIcon-command" style="margin-right: 10rpx;"></text> 保存
					</button>
				</view>
			</view>
			
			
			<movable-area class="movable-area1">
				<movable-view class="movable-view" :x="x" :y="y" direction="all">
					
					
					<view @click="search" class="custom-style" style="width: 90px; height: 70px; color: #fff; border-radius: 10rpx;">
						
						<view style="font-size: 16px;display: flex; flex-direction: column;text-align: center;">
							<!-- <text class="cuIcon-scan text-xxl" ></text> -->
							<!-- <u-icon style="margin: auto;" name="/static/jeesite/sm1.png" size="80"></u-icon> -->
							<text class="cuIcon-camera text-xxls text-blue"></text>
							<view class="text-blue">条码识别</view>
						</view>
					</view>
					<!-- <button size="mini" >
						
					</button> -->
				</movable-view>
			</movable-area>
		</view>
	</view>
</template>

<script>
	import config from '@/common/config.js'
	import util from '@/common/fire.js'
	export default {
		data() {
			return {
				model: { 
					needSoTrack:'1' //是否启用强制追溯
				},
				soOutList:[{
					cbatch:'',
					iqty:'',
				}],
				palletRdsList:[],
				invFlag: false,
				focus: false,
				barCode: "",
				carType: '',
				carVenSelectList: [],
				cstatus: '',
				children: [],
				show: false,
				isSubmitting: false,
				x: 650, //x坐标
				y: 650, //y坐标
				fhQty:''	
			}
		},
		 computed: {
		    sortedChildren() {
		      return this.children.slice().sort((a, b) => {
		        if (a.cbatch < b.cbatch) return -1;
		        if (a.cbatch > b.cbatch) return 1;
		        return 0;
		      });
		    }
		  },
		onLoad(params) {
			this.getForm(params.id)
			uni.$on('hwObjs', (data,index) => {		
				console.log('data',data)
				if(!index){
					let arr = [...this.children]
					
					this.model.posName = data[0]['name']
					this.model.posCode = data[0]['id']
					this.$forceUpdate()
				}else{
					let arr = [...this.soOutList]
					console.log(arr,'arr===1',index)
					const flag = arr.every(item => item.cbatch != data[0]['cbatch']);
					if (flag) {
						this.soOutList[index]['cbatch'] = data[0]['cbatch'];
						this.soOutList[index]['ichangeRate'] = data[0]['zbl'];
						this.soOutList[index]['rgrade'] = data[0]['rgrade'];
						this.soOutList[index]['iquantity'] = data[0]['iquantity'];
						this.soOutList[index]['posCode'] = this.model.posCode;
						this.soOutList[index]['invCode'] = this.model.invCode;
						this.soOutList[index]['parentId'] = params.id;
						this.soOutList[index]['iqty'] = null;
						this.soOutList[index]['inum'] = null;
						this.$forceUpdate()
					}else{
						this.$refs.jsError.showError('', '已存在相同批次！', 'error');
					}
					
				}
			})
			
			uni.$on('cbatchObj', (data,index) => {
				console.log(index,'index',data,this.model)
				// this.model.invCode2 = data.invCode
				if (this.model && this.model.basWare) {
					let arr = [...this.soOutList]
					if (this.model.basWare.bwhpos != '1') {
						const flag = arr.every(item => item.cbatch != data.cbatch);
						if (flag) {
							if (!this.soOutList[index]) {
								this.$set(this.soOutList, index, {}); // 使用 Vue.set 来确保响应性
							}
							this.soOutList[index]['cbatch'] = data.cbatch;
							this.soOutList[index]['ichangeRate'] = data.zbl;
							this.soOutList[index]['rgrade'] = data.rgrade;
							this.soOutList[index]['iquantity'] = data.iquantity;
							this.soOutList[index]['posCode'] = this.model.posCode;
							this.soOutList[index]['invCode'] = this.model.invCode;
							this.soOutList[index]['parentId'] = params.id;
							this.soOutList[index]['iqty'] = null;
							this.soOutList[index]['inum'] = null;
						}else{
							this.$refs.jsError.showError('', '已存在相同批次！', 'error');
						}
						this.$forceUpdate()
					}else if (this.model.basWare.bwhpos == '1') {
						const flag = arr.every(item =>item.cbatch != data.cbatch);
						console.log(flag,'flag')
						if (flag) {
							if (!this.soOutList[index]) {
								this.$set(this.soOutList, index, {}); // 使用 Vue.set 来确保响应性
							}
							this.soOutList[index]['cbatch'] = data.cbatch;
							this.soOutList[index]['ichangeRate'] = data.zbl;
							this.soOutList[index]['rgrade'] = data.rgrade;
							this.soOutList[index]['iquantity'] = data.iquantity
							this.soOutList[index]['posCode'] = this.model.posCode;
							this.soOutList[index]['invCode'] = this.model.invCode;
							this.soOutList[index]['parentId'] = params.id;
							this.soOutList[index]['iqty'] = null;
							this.soOutList[index]['inum'] = null;
						}else{
							this.$refs.jsError.showError('', '相同货位，批次不能相同', 'error');
						}
						this.$forceUpdate()
					}
					
				}
				
				
			})
		},
		onShow() {
		},
		methods: {
			init(){
				uni.showModal({
					title: '温馨提示',
					content: '初始化，页面信息清空，请谨慎操作！',
					confirmColor: '#F54E40',
					success: (res) => {
						if (!res.confirm) {
							return false;
						}
						this.children = []
						this.soOutList = [{
							iqty : '',
							cbatch: ''
						}]
						if(this.model.ibatch){
							this.soOutList[0]['cbatch'] = this.model.ibatch 
						}
						this.model.posName = ''
						this.model.posCode = ''
					}
				})
			},
			historyList(id){
				uni.navigateTo({
					url: '/pages/mf/zc/historyList?id='+id,
					events: {
						// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
						Filter(data) {
							console.log(data)
						}
					},
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						//res.eventChannel.emit('initfilter', that.queryData)
					}
				})
			},
			xzhw(index,item){
				if(!this.model.posName){
					this.$refs.jsError.showError('', '请先确定货位', 'error');
					return;
				}
				if(this.model.needSoTrack == 1){
					this.$refs.jsError.showError('', '已开启强制追溯，请扫托盘码', 'error');
					return;
				}
				if(!!item.cbatch) return
				let data = {
					cinvCode:this.model.basInv.invCode,
					cwhCode:this.model.basWare.cwhcode,
					cposCode:this.model.posCode,
				}
				data.cinvCode ? data.bmustInv = 1 : ''
				data.cwhCode ? data.bmustWh = 1 : ''
				// console.log(data,'data')
				uni.navigateTo({
					url: '/pages/mf/zc/hwXz?data=' + JSON.stringify(data)+'&index='+ index,
					events: {
						Filter(data) {
									
						}
					},
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						//res.eventChannel.emit('initfilter', that.queryData)
					}
				})
				this.$forceUpdate()
			},
			xzCbatch(index,item){
				
				if(!!item.cbatch) return
				if(this.model.needSoTrack == 1){
					this.$refs.jsError.showError('', '已开启强制追溯，请扫托盘码', 'error');
					return;
				}
				// 现存量表
				let data = {
					invCode:this.model.basInv.invCode,
					whCode:this.model.basWare.cwhcode
				}
				data.invCode ? data.bmustInv = 1 : ''
				data.whCode  ? data.bmustWh = 1 : ''
				uni.navigateTo({
					url: '/pages/mf/zc/cbatchXz?index='+index + '&data=' + JSON.stringify(data),
					events: {
						// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
						Filter(data) {
							console.log(data)
						}
					},
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						//res.eventChannel.emit('initfilter', that.queryData)
					}
				})
				
				
			},
			makeSound(name){
				console.log("=====testClick=====");
				let src = '/static/'+name+'.mp3';
				//实例化声音  
				const Audio = uni.createInnerAudioContext();
				Audio.autoplay = true;
				Audio.src = src; //音频地址  
				Audio.play(); //执行播放  
				Audio.onError((res) => {
				});
				Audio.onPause(function() {
					console.log('end');
					Audio.destroy();
				});
			},
			cbatchChange(e, item, index) {
				item.cbatch = e
				if (this.model.basWare.bwhpos != '1') {
					let newArr = this.children.map(item => item.cbatch)
					let arrSet = new Set(newArr)
					if (arrSet.size != newArr.length && this.model.basInv.binvbatch == '1') {
						this.$nextTick(() => {
							item.cbatch = ''
						})
						this.$forceUpdate()
						this.$refs.jsError.showError('', '批次不能相同！', 'error');
						return;
					}
				}

				if (this.model.basWare.bwhpos == '1') {
					let f = this.hasDuplicateIdAndName(this.children)
					if (f) {
						this.$nextTick(() => {
							item.cbatch = ''
						})
						this.$forceUpdate()
						this.$refs.jsError.showError('', '相同货位，批次不能相同！', 'error');
						return;
					}

				}

			},
			iqtyChange(e, item, index) {
				// e = e.match(/^\d*(\.?\d{0,4})/g)[0]
				item.iqty = util.toNumberFixed(Number(e))
				if (this.model.basInv.bfixExch != '0' ) {
					item.ichangeRate ? item.ichangeRate = item.ichangeRate : item.ichangeRate = 0
					// item.inum = (e / item.ichangeRate).toFixed(config.scaleNum)
					item.inum = util.toNumberFixed(e / item.ichangeRate)
				}
				this.fhQty = this.soOutList.reduce((prev, next) => {
					 const currentQty = Number(next.iqty) || 0;
					 return Number(prev) + currentQty;
				}, 0);
				// this.fhQty = this.fhQty.toFixed(config.scaleQty)
				this.fhQty =util.toNumberFixed(this.fhQty)
				this.$forceUpdate()
			},
			inumChange(e, item, index) {
				// e = e.match(/^\d*(\.?\d{0,4})/g)[0]
				item.inum = e
				if (this.model.basInv.bfixExch == '1' || this.model.basInv.bfixExch == '2') {
					item.ichangeRate ? item.ichangeRate = item.ichangeRate : item.ichangeRate = 0
					// item.iqty = (e * item.ichangeRate).toFixed(config.scaleQty)
					item.iqty = util.toNumberFixed(e * item.ichangeRate)
				}
				this.fhQty = this.soOutList.reduce((prev, next) => {
					 const currentQty = Number(next.iqty) || 0;
					 return Number(prev) + currentQty;
				}, 0);
				this.fhQty =util.toNumberFixed(this.fhQty)
				this.$forceUpdate()
			},
			getForm(id) {
				this.$u.api.mffh.soFhForm({
					id
				}).then(res => {
					// console.log(res,'res')
					this.model = res.fhC
					this.model.needSoTrack = '1'
					
					if(!!this.model.ibatch){
						this.soOutList[0]['cbatch'] = this.model.ibatch 
					}
					this.$u.api.mffh.wmsPalletSendRdsListData({
						carplanCid:id
					}).then(res=>{
						// console.log('res',res)
						this.palletRdsList = res.list
					})
					this.$forceUpdate()
					
					// this.model.basWare.bwhpos = '0' 
					// this.model.basInv.binvbatch ='0'
					// this.model.basInv.bfixExch = '2'
					// this.model.ichangeRate= 2


					// if (this.model.basWare.bwhpos == '0' && this.model.basInv.binvbatch == '0') {
					// 	this.children.push({})
					// }
				})
			},
			confirm() {
				this.focus = false
				console.log(this.barCode, 'barCode')
				this.commonSearch(this.barCode)
				// // await this.$nextTick();
				// setTimeout(() => {
				// 	this.focus = true;
				// }, 500);
			},
			search() {
				var _that = this;
				_that.focus = false
				if(this.vuex_config.deviceType === this.vuex_config.deviceTypeAPP){
					const wechat = uni.getSystemInfoSync()?.host?.env
					if (wechat === 'WeChat') {
					    uni.scanCode({
					    	scanType: ["barCode", "qrCode"],
					    	// onlyFromCamera: true,
					    	success: async (res) => {
					    		_that.barCode = res.result;
					    		await _that.commonSearch(res.result)
					    	},
					    });
					} else {
						var mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module");
						console.log('mpaasScanModule',mpaasScanModule)
						
						mpaasScanModule.mpaasScan({
							// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
							'scanType':  ['qrCode','barCode'],
							// 是否隐藏相册，默认false不隐藏
							'hideAlbum': false,
							//ios需要设置这个参数，只支持中英文 zh-Hans、en，默认中文                       
							'language' : 'en',
							//相册选择照片识别错误提示(ios)
							'failedMsg': '未识别到二维码，请重试',
							//Android支持全屏需要设置此参数
							'screenType': 'full'              
						},
						async (ret) => {
							console.log('ret',ret)
							if(ret.resp_message == 'success'){
								_that.barCode = ret.resp_result;
								await _that.commonSearch(ret.resp_result)
							}
						})
					}
				}else if(this.vuex_config.deviceType === this.vuex_config.deviceTypePDA){
					this.handleFocus()
				}	
			},
			
			async commonSearch(barCode) {
				
				let that = this
				
				//扫货位
				if (util.checkBarcode(barCode, this.vuex_config.PositionPrefix) && that.model.basWare.bwhpos == '1') {
					const that = this
					if(!!this.model.posName){
						uni.showModal({
							title: '温馨提示',
							content: '更换货位，批次信息会清空，请谨慎操作！',
							confirmColor: '#F54E40',
							success: (res) => {
								if (!res.confirm) {
									return false;
								}
								this.$u.api.mf.getBarInfo({
									barCode,
									whCode: this.model.basWare.cwhcode
								}).then(res => {
									if (res.result == 'true') {
										if (!res.error) {
								
											if (that.model.basInv.binvbatch == '1') {
												this.model.posName  = res.data.name
												this.model.posCode  = res.data.code
												this.children = [],
												this.soOutList = [{
													iqty : '',
													cbatch: ''
												}]
												setTimeout(()=>{
													that.makeSound("cg");
												},500)
												that.$u.toast('成功！');
											} 
								
										} else {
											setTimeout(()=>{
												that.makeSound("sb");
											},500)
											that.$refs.jsError.showError('', res.error, 'error');
										}
									} else {
										setTimeout(()=>{
											that.makeSound("sb");
										},500)
										that.$refs.jsError.showError('', res.message, 'error');
									}
								})
							}
						})
					}else{
						this.$u.api.mf.getBarInfo({
							barCode,
							whCode: this.model.basWare.cwhcode
						}).then(res => {
							if (res.result == 'true') {
								if (!res.error) {
									if (that.model.basInv.binvbatch == '1') {
										this.model.posName  = res.data.name
										this.model.posCode  = res.data.code
										setTimeout(()=>{
											that.makeSound("cg");
										},500)
										that.$u.toast('成功！');
									}
						
								} else {
									setTimeout(()=>{
										that.makeSound("sb");
									},500)
									that.$refs.jsError.showError('', res.error, 'error');
								}
							} else {
								setTimeout(()=>{
									that.makeSound("sb");
								},500)
								that.$refs.jsError.showError('', res.message, 'error');
							}
						})
					}
					
					
					
					
				} else if (util.checkBarcode(barCode, this.vuex_config.cbatchPrefix)) {
					if(this.model.basWare.bwhpos == '1'&&!this.model.posName){
						this.$refs.jsError.showError('', '请先确定货位', 'error');
						return;
					}
					if (this.model.needSoTrack == 1) {
						this.$refs.jsError.showError('', '开启强制追溯，请扫托盘码', 'error');
						setTimeout(() => {
							this.barCode = '';
						}, 500);
						return;
					} 
					let data = {
						barCode,
						needCheckStore:1,
					};
					this.model.posCode ? data.posCode = this.model.posCode : '';
					this.model.basWare && this.model.basWare.cwhcode ? data.whCode = this.model.basWare.cwhcode : '';
					
					this.$u.api.mf.getBarInfo(data).then(res => {
						if(res.result == 'true'){
							if (!res.error) {
								if(res.data.basInv && res.data.basInv.invCode == that.model.invCode){
									that.invFlag = true
									that.$u.toast('验证成功！');
									setTimeout(()=>{
										that.makeSound("cg");
									},500)
									const flag = this.soOutList.every(item =>  item.cbatch != res.data.bizKey);
									console.log(this.soOutList,'this.soOutList')
									if(this.soOutList.length == 0 || flag){
										if(this.soOutList.length == 1 && !that.soOutList[0]['cbatch']){
											that.soOutList[0]['cbatch'] = res.data.bizKey;
											that.soOutList[0]['ichangeRate'] = res.data.batchInfo.ichangeRate;
											that.soOutList[0]['rgrade'] = res.data.batchInfo.cgradeDesc;
											that.soOutList[0]['posCode'] =  that.model.posCode;
											that.soOutList[0]['invCode'] = that.model.invCode;
										}else{
											that.soOutList.unshift({
												cbatch:res.data.bizKey,
												ichangeRate:res.data.batchInfo.ichangeRate,
												rgrade: res.data.batchInfo.cgradeDesc,
												posCode : that.model.posCode,
												invCode : that.model.invCode
											})
										}
									}else{
										that.soOutList.unshift({
											cbatch:res.data.bizKey,
											ichangeRate:res.data.batchInfo.ichangeRate,
											rgrade: res.data.batchInfo.cgradeDesc,
											posCode : that.model.posCode,
											invCode : that.model.invCode
										})
									}
								}else{
									setTimeout(()=>{
										that.makeSound("sb");
									},500)
									that.$refs.jsError.showError('', '存货验证失败！', 'error');
								}
							} else {
								setTimeout(()=>{
									that.makeSound("sb");
								},500)
								that.$refs.jsError.showError('', res.error, 'error');
							}
						}else{
							setTimeout(()=>{
								that.makeSound("sb");
							},500)
							that.$refs.jsError.showError('', res.message, 'error');
						}
					})
				} else if (util.checkBarcode(barCode, this.vuex_config.TuoPanPrefix)) {
					if(this.model.basWare.bwhpos == '1'&&!this.model.posName){
						this.$refs.jsError.showError('', '请先确定货位', 'error');
						setTimeout(() => {
							this.barCode = '';
						}, 500);
						return;
					}
					const matchingItem = this.children.find(item => this.vuex_config.TuoPanPrefix + item.cbatch === barCode);
					
					if(matchingItem){
						this.$u.toast('请勿重复扫码');
						if(this.vuex_config.deviceType === this.vuex_config.deviceTypePDA){
							this.handleFocus()
						}
						setTimeout(() => {
							this.barCode = '';
							that.makeSound("sb");
						}, 500)
						return false;
					}
					this.$u.api.mf.getBarInfo({
						barCode,
						needCheckStore:1,
						posCode:this.model.posCode,
						needCheckTuoPan:1,
						barType:this.vuex_config.TuoPanBarType,
						whCode:this.model.basWare.cwhcode
					}).then(res => {
						if(res.result == 'true'){
							if (!res.error) {
								if(res.data.basInv && res.data.basInv.invCode == that.model.invCode){
									that.invFlag = true
									that.$u.toast('验证成功！');
									setTimeout(()=>{
										that.makeSound("cg");
									},500)
									const flag = this.children.every(item => item.tuopan && item.tuopan != res.data.bizKey);
									if(this.children.length == 0 || flag){
										that.children.unshift({
											tuopan: res.data.batchInfo.u8Batch,
											cbatch:res.data.batchInfo.cbatch,
											id:''
											// 	posName: res.data.name,
										})
										
										const matchingItem = this.soOutList.find(item => item.cbatch === res.data.batchInfo.u8Batch);
										if(matchingItem){
											matchingItem.tpSeqNumber = matchingItem.tpSeqNumber +1;
											this.$forceUpdate()
										}else{
											if(this.soOutList.length == 1 && !that.soOutList[0]['cbatch']){
												that.soOutList[0]['cbatch'] = res.data.batchInfo.u8Batch;
												that.soOutList[0]['ichangeRate'] = res.data.batchInfo.ichangeRate;
												that.soOutList[0]['rgrade'] = res.data.batchInfo.cgradeDesc;
												that.soOutList[0]['posCode'] =  that.model.posCode;
												that.soOutList[0]['invCode'] = that.model.invCode;
												that.soOutList[0]['bizKey'] = res.data.bizKey;
												that.soOutList[0]['tpSeqNumber'] = 1
											}else{
												that.soOutList.unshift({
													cbatch:res.data.batchInfo.u8Batch,
													ichangeRate:res.data.batchInfo.ichangeRate,
													rgrade: res.data.batchInfo.cgradeDesc,
													posCode : that.model.posCode,
													invCode : that.model.invCode,
													tpSeqNumber : 1,
													bizKey:res.data.bizKey
												})
											}
										}	
									}else{
										that.soOutList.unshift({
											cbatch:res.data.batchInfo.u8Batch,
											ichangeRate:res.data.batchInfo.ichangeRate,
											rgrade: res.data.batchInfo.cgradeDesc,
											posCode : that.model.posCode,
											invCode : that.model.invCode,
											tpSeqNumber : 1,
											bizKey:res.data.bizKey
										})
									}
									
									
								}else{
									setTimeout(()=>{
										that.makeSound("sb");
									},500)
									that.$refs.jsError.showError('', '存货验证失败！', 'error');
								}								
							} else {
								setTimeout(()=>{
									that.makeSound("sb");
								},500)
								that.$refs.jsError.showError('', res.error, 'error');
							}
						}else{
							setTimeout(()=>{
								that.makeSound("sb");
							},500)
							that.$refs.jsError.showError('', res.message, 'error');
						}
					})
				} else if (util.checkBarcode(barCode, this.vuex_config.InventoryPrefix)) {
					this.$u.api.mf.getBarInfo({
						barCode,
					}).then(res => {
						if(res.result == 'true'){
							if (!res.error) {
								if(res.data.basInv && res.data.basInv.invCode == that.model.invCode){
									that.invFlag = true
									setTimeout(()=>{
										that.makeSound("cg");
									},500)
									that.$u.toast('验证成功！');
								}else{
									setTimeout(()=>{
										that.makeSound("sb");
									},500)
									that.$refs.jsError.showError('', '验证失败！', 'error');
								}
								
							} else {
								setTimeout(()=>{
									that.makeSound("sb");
								},500)
								that.$refs.jsError.showError('', res.error, 'error');
							}
						}else{
							setTimeout(()=>{
								that.makeSound("sb");
							},500)
							that.$refs.jsError.showError('', res.message, 'error');
						}
					})
				} else {
					setTimeout(()=>{
						that.makeSound("sb");
					},500)
					this.$refs.jsError.showError('', '请扫描正确的条码！', 'error');
				}
				if(this.vuex_config.deviceType === this.vuex_config.deviceTypePDA){
					setTimeout(() => {
						this.barCode = '';
						this.handleFocus()
					}, 500);
				}else{
					setTimeout(() => {
						this.barCode = '';
					}, 500);
				}
				

			},
			handleFocus() {
				var _that = this;
				_that.focus = false;
				setTimeout(() => {
					_that.focus = true;
				}, 500)
			},
			// 判断输入的值children的每一项的iqty的值的和不能大于model的syInQty的值，小数点保留4位
			iqtyChange2(inputValue, item, index) {
				// 检查输入值是否为有效的数字
				const parsedInputValue = parseFloat(inputValue);
				if (isNaN(parsedInputValue) || inputValue === '') {
					return;
				}
				// 计算除了当前修改项之外的所有子项的iqty总和
				let totalIqty = 0;
				for (const child of this.children) {

					if (child.index !== index) {
						totalIqty += parseFloat(child.iqty) || 0; // 使用0作为默认值以避免NaN
					}
				}

				// 加上当前修改项的值
				totalIqty += parsedInputValue;

				// 检查总和是否超过model.syInQty
				const syInQty = parseFloat(this.model.syInQty);
				console.log(totalIqty, 'totalIqty')
				console.log(syInQty, 'syInQty')
				if (totalIqty > syInQty) {
					this.$refs.jsError.showError('', '数量总和不能超过剩余入库数量！', 'error');
					return;
				}

				// 如果没有超过，则更新当前子项的iqty值，保留四位小数
				// this.$set(this.children[index], 'iqty', parsedInputValue);

			},
			delDetail(item, index) {
				let that = this
				uni.showModal({
					title: '删除提示',
					content: '是否删除此货位信息？',
					confirmColor: '#F54E40',
					success: (res) => {
						if (!res.confirm) {
							return false;
						}
						const indexItem = that.soOutList.find(listItem => listItem.cbatch === item.tuopan);
						if(indexItem){
							indexItem.tpSeqNumber -= 1
							if(indexItem.tpSeqNumber <=0){
								if (index !== -1) {
									that.soOutList.splice(index, 1);
									that.fhQty = that.soOutList.reduce((prev, next) => {
										 const currentQty = Number(next.iqty) || 0;
										 return Number(prev) + currentQty;
									}, 0);
									that.fhQty =util.toNumberFixed(that.fhQty)
									that.$forceUpdate()
								}
							}
							
							console.log('index',index)
							that.children.splice(index, 1);
						}
						
						that.$forceUpdate()
					}
				})
			},
			delsoOutDetail(item,index){
				console.log('item',item)
				let that = this
				uni.showModal({
					title: '删除提示',
					content: '是否删除此数量信息？',
					confirmColor: '#F54E40',
					success: (res) => {
						if (!res.confirm) {
							return false;
						}
						const filteredChildren = that.children.filter(listItem => listItem.tuopan !== item.cbatch);
						that.children = filteredChildren;
						
						that.soOutList.splice(index, 1);
						that.fhQty = that.soOutList.reduce((prev, next) => {
							 const currentQty = Number(next.iqty) || 0;
							 return Number(prev) + currentQty;
						}, 0);
						// that.fhQty = that.fhQty.toFixed(config.scaleQty)
						that.fhQty =util.toNumberFixed(that.fhQty)
						
						that.$forceUpdate()
					}
				})
			},
			hwxz() {
				const that = this
				if(!!this.model.posName){
					uni.showModal({
						title: '温馨提示',
						content: '更换货位，批次信息会清空，请谨慎操作！',
						confirmColor: '#F54E40',
						success: (res) => {
							if (!res.confirm) {
								return false;
							}
							uni.navigateTo({
								url: '/pages/mf/cgrk/hwXz?whcode=' + this.model.basWare.cwhcode,
								events: {
									Filter(data) {
							
									}
								},
								success: function(res) {
									// 通过eventChannel向被打开页面传送数据
									//res.eventChannel.emit('initfilter', that.queryData)
								}
							})
							
							that.$forceUpdate()
							
						}
					})
				}else{
					uni.navigateTo({
						url: '/pages/mf/cgrk/hwXz?whcode=' + this.model.basWare.cwhcode,
						events: {
							Filter(data) {
					
							}
						},
						success: function(res) {
							// 通过eventChannel向被打开页面传送数据
							//res.eventChannel.emit('initfilter', that.queryData)
						}
					})
					that.$forceUpdate()
				}
				
				
				
				

			},
			GoaddChild() {
				this.children.unshift({})
			},
			GoaddsoOutListChild(){
				this.soOutList.unshift({ 
					iqty : '',
					cbatch : this.model.ibatch ? this.model.ibatch : ''
				})
			},
			hasDuplicateIdAndName(arr) {
				const seen = new Map();

				for (const item of arr) {
					const key = `${item.posCode}-${item.cbatch}`;
					if (seen.has(key)) {
						return true; // 发现重复的 id 和 name 组合
					}
					seen.set(key, true);
				}
				return false; // 没有发现重复的 id 和 name 组合
			},
			submit(data) {
				
				if(!this.invFlag){
					this.$refs.jsError.showError('','请先验证存货！','error');
					return;
				}
				if (this.model.basWare.bwhpos == '1' &&!this.model.posName) {
					this.$refs.jsError.showError('', '请先添加货位信息！', 'error');
					return;
				} 
				if (this.model.needSoTrack == 1 &&!this.children.length) {
					this.$refs.jsError.showError('', '开启强制追溯，请先添加托盘信息！', 'error');
					return;
				} 
				
				// this.soOutList 数组里面的iqty 累加
				let iqtySum = this.soOutList.reduce((prev, next) => {
					 const currentQty = Number(next.iqty) || 0;
					 return prev + currentQty;
				}, 0);
				// iqtySum = iqtySum.toFixed(config.scaleQty)
				iqtySum =util.toNumberFixed(iqtySum)
				if (iqtySum > this.model.syFhQty) {
					this.$refs.jsError.showError('', `本次出库总数${iqtySum}不能大于${this.model.syFhQty}`, 'error');
					return;
				}
				
				const isValid = this.soOutList.every(item => {
				  return item && typeof item.cbatch === 'string' && item.cbatch.trim() !== '';
				});
				if(this.model.basInv.binvbatch == '1'&&!isValid){
					this.$refs.jsError.showError('', '每一项都必须有一个有效的 批次 值！', 'error');
					return false; 
				}
				console.log(this.soOutList,'this.soOutList===');
				
				const isIqtyValid = this.soOutList.every(item => {
				  if (item) {
 				    const iqtyValue = parseFloat(item.iqty);
 				    return !isNaN(iqtyValue) && iqtyValue > 0;
 				  }
				  return false;
				});
				if(!isIqtyValid){
					this.$refs.jsError.showError('', '每一项都必须有一个大于0的有效的 发货数量 值！', 'error');
					return false; 
				}
				let hasError = false;
				let hasErrorMsg = ''
				
				this.soOutList.some(item => {
				  if(this.model.basWare.bwhpos == '1'&&this.model.posName){
					  item.posName = this.model.posName
					  item.posCode = this.model.posCode
				  }
				  if (item.iquantity && item.iquantity < item.iqty && ((this.model && this.model.basWare && this.model.basWare.bwhpos === '1') || this.model?.basInv?.binvbatch === '1')) {
				    hasErrorMsg = `批次${item.cbatch}出库数${item.iqty}不能大于存量总数${item.iquantity}`
				    hasError = true; // 设置错误标志
				    return true; // 中断 some 遍历
				  }
				  return false;
				});
				
				if (hasError) {
				  this.$refs.jsError.showError('', hasErrorMsg , 'error');
				  return false;
				}
				
				if(!!data){
					this.model.fhStatus = data
				}
				this.children.forEach(child => {
				  child.ichangeRate = this.model.ichangeRate
				});
				this.model.soOutList = this.soOutList
				this.model.cposition = this.model.posCode
				if(this.model.needSoTrack == 1){
					this.model.palletRdsList = this.children
				}
				this.model.sumFhQty = iqtySum	
				if (this.isSubmitting) {
						return;
				} else {
					this.isSubmitting = true;
					var data = {
						...this.model,
					};
					try {
					  this.$u.api.mffh.wmsSoOutRdsSaveRds(data).then(res => {
					  	if (res.result == 'true') {
					  		this.$u.toast(res.message);
					  		setTimeout(() => {
					  			// uni.$emit('refreshData');
					  			uni.navigateBack({
					  				delta: 1
					  			})
					  		}, 500)
					  	} else {
					  		this.$refs.jsError.showError('', res.message, 'error');
					  	}
					  
					  });
					} catch (error) {
						this.$refs.jsError.showError('', error.message, 'error');
					} finally {
					  this.isSubmitting = false;
					} 	
				}	
			},
		}
	}
</script>
<style scoped lang="scss">
	$all_width: 96rpx;
	$all_height: 96rpx;
	.movable-area1 {
		z-index: 888;
		height: 97vh;
		width: 650rpx;
		position: fixed;
		right: $all_width;
		top: -($all_height + 100rpx);
		pointer-events: none; //此处要加，鼠标事件可以渗透
		.movable-view {
			width: $all_width;
			height: $all_height;
			pointer-events: auto; //恢复鼠标事件
			image {
				width: $all_width;
				height: $all_height;
			}
		}
	}
	
	.footer {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		background: #fff;z-index: 999;
		border-top: 1px solid #eee;
	}
	.text-xxls{
		font-size:60rpx;
	}
	.hide {
		display: none;
	}

	.cu-bar {
		min-height: 60px;
	}


	.foot {
		background: #fff;
	}

	.cu-item {
		flex-wrap: wrap;
		padding: 8px 15px !important;
	}

	.title {
		width: 280rpx;
	}
	.title1{
		width: 150rpx;
	}

	.cu-modal-slot {
		margin: 42rpx 32rpx 20rpx 32rpx;
		height: 100vh;
		position: relative;
	}

	.cu-modal-header {
		font-size: 38rpx;
		color: #3d3d3d;
		line-height: 40rpx;
		text-align: left;
		font-weight: 500;
	}

	.button {
		font-size: 32rpx;
		color: #666666;
		line-height: 40rpx;
		padding: 12rpx 40rpx;
		margin-bottom: 20rpx;
		background: #e2e2e2;
		border-radius: 180rpx;
	}

	.button:hover {
		background: #3e97b0;
		color: #ffffff;
	}

	.cu-modal-search-title {
		margin: 32rpx 0rpx 20rpx 0rpx;
		text-align: left;
		font-size: 32rpx;
		color: #999999;
		line-height: 40rpx;
	}

	.cu-dialog {
		background: #ffffff;
	}

	.cu-modal-footer {
		padding: 32rpx 32rpx;
		width: 100%;
	}

	.cu-btn {
		font-size: 16px;
		height: 40px;
		width: 50%;
	}

	.bg-confirm {
		background: #3e97b0;
		color: #ffffff;
	}
	uni-modal {
	  z-index: 999999 !important;
	}
	.text-xxl{
		font-size: 72rpx !important;
	}
</style>